﻿using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Debugger.Interop;
using Red.Scripts.Debugger.Defines;

namespace Red.Scripts.Debugger.Remote.Debugger.Breakpoint.Resolution
{
    /// <summary>
    /// A breakpoint that corresponds to a line of code
    /// </summary>
    public class Standard : IDebugBreakpointResolution2
    {
        #region Public Properties

        public StandardCodeContext CodeContext { get; private set; }

        #endregion Public Properties

        #region C-Tor

        public Standard( StandardCodeContext codeContext )
        {
            CodeContext = codeContext;
        }

        #endregion C-Tor

        #region IDebugBreakpointResolution2

        public int GetBreakpointType( enum_BP_TYPE[] pBPType )
        {
            pBPType[ 0 ] = enum_BP_TYPE.BPT_CODE;

            return Constants.S_OK;
        }

        public int GetResolutionInfo( enum_BPRESI_FIELDS dwFields, BP_RESOLUTION_INFO[] pBPResolutionInfo )
        {
            if ( Utility.HasFlag( dwFields, enum_BPRESI_FIELDS.BPRESI_BPRESLOCATION ) )
            {
                BP_RESOLUTION_LOCATION location = new BP_RESOLUTION_LOCATION();
                location.bpType = (uint)enum_BP_TYPE.BPT_CODE;

                // The debugger will not QI the IDebugCodeContex2 interface returned here. We must pass the pointer
                // to IDebugCodeContex2 and not IUnknown.
                location.unionmember1 = Marshal.GetComInterfaceForObject( CodeContext, typeof( IDebugCodeContext2 ) );

                pBPResolutionInfo[ 0 ].bpResLocation = location;
                pBPResolutionInfo[ 0 ].dwFields |= enum_BPRESI_FIELDS.BPRESI_BPRESLOCATION;
            }

            if ( Utility.HasFlag( dwFields, enum_BPRESI_FIELDS.BPRESI_PROGRAM ) )
            {
                pBPResolutionInfo[ 0 ].pProgram = CodeContext.Program;
                pBPResolutionInfo[ 0 ].dwFields |= enum_BPRESI_FIELDS.BPRESI_PROGRAM;
            }

                return Constants.S_OK;
        }

        #endregion IDebugBreakpointResolution2
    }
}
